/*
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */



#include <string.h>


#include <panel-applet.h>
#include <gtk/gtk.h>

#include <gdk/gdkkeysyms.h>

#   include <X11/Xlib.h>
#   include <X11/Xutil.h>
#   include <gdk/gdkx.h>

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

#include "gelogger.h"

typedef struct
{
    
    t_gelogger      gelogger;
    PanelApplet*    applet;
    // widgets of the applet
    GtkWidget*     editbox;
    GtkWidget*     icon;
    GtkWidget*     hbox;
    GtkWidget*     noicon;

} t_applet;    
    

#define WORKLOG "/home/sawtscge/work.log"
#define CMD "tail -n 10 "
#define IDLE_SPAN 1

static void menu_show_bottom_entries( BonoboUIComponent *component, gpointer wl, const char *verb );

static const char Context_menu_xml [] =
   "<popup name=\"button3\">\n"
   "   <menuitem name=\"Show last entries\" "
   "             verb=\"show_bottom_entries\" "
   "           _label=\"_Entries\"\n"
   "          pixtype=\"stock\" "
   "          pixname=\"gtk-properties\"/>\n"
   "</popup>\n";

static const BonoboUIVerb gelogger_menu_verbs [] = {
        BONOBO_UI_VERB ("show_bottom_entries", menu_show_bottom_entries ),
        BONOBO_UI_VERB_END
};


static void finalize( 
		PanelApplet *applet,
		 t_applet *wl )
{
	report( &wl->gelogger, time(NULL), "stop working detected on exit" );
}

static gboolean close_bottom_entries( gpointer data )
{
	t_applet* wl = (t_applet*)data;

//	gtk_widget_destroy( wl->toplist );

//	wl->toplist = NULL;
	
//  g_source_remove( wl->timeout );
	
//	wl->timeout = 0;

	return FALSE;
}

static gboolean button_close_bottom_entries( GtkButton* _button, gpointer* wl )
{
	close_bottom_entries( wl );
}


static void menu_show_bottom_entries( BonoboUIComponent *component, gpointer wl, const char *verb )
{
    //show 10 last entries without timeout(last param)
//	show_bottom_entries( wl, 0 );
}


static gboolean want_input( GtkEntry       *entry,
		 GdkEventButton    *event,
		 t_applet *wl )
{
    panel_applet_request_focus ( PANEL_APPLET( wl->applet ), event->time );

    return FALSE;
}

static gboolean command_key(GtkEntry   *entry,
		   GdkEventKey *event,
		   t_applet  *wl )
{

	guint key = event->keyval;
	
	if(key == GDK_Return
	    || key == GDK_KP_Enter
	    || key == GDK_ISO_Enter
	    || key == GDK_3270_Enter)
	{

		report( &(wl->gelogger), time(NULL), (char*)gtk_entry_get_text( entry  ) );
        
        //show list of last ten entries, with timeout(last param)
//		show_bottom_entries( wl, 1 );
        //and remove all entered text in text field 
		gtk_entry_set_text(entry, (gchar*)"" );
	}

	return FALSE;
}

static gboolean check_absence( gpointer data )
{
	t_applet* wl = (t_applet*)data;

    gelogger_check_absence( &(wl->gelogger) );

	if( wl->gelogger.nologcounter > wl->gelogger.nologtime && 0 == wl->gelogger.iconswitcher )
	{
		gtk_widget_hide(  wl->icon );
		GtkWidget* hbwg = (GtkWidget*)(wl->hbox);
		gtk_widget_set_size_request( wl->hbox, hbwg->allocation.width, hbwg->allocation.height );
		wl->gelogger.iconswitcher = 1;
	}
	else if( wl->gelogger.iconswitcher )
	{
		gtk_widget_show(  wl->icon );
		wl->gelogger.iconswitcher = 0;			
	}

	return TRUE;
}

void _debug( const char* text )
{
   printf( "GELOGGER: %s\n", text );
    
    
}

static gboolean gelogger_applet_fill(
		PanelApplet *applet,
		const gchar *iid,
		gpointer data )
{
    _debug( "entered gelogger_applet_fill" );
    _debug( iid );
    
	if (strcmp (iid, "OAFIID:Gelogger_Applet") != 0)
		return FALSE;

    _debug( "alloocate\n" );
	t_applet* wl = g_new0( t_applet, 1 );

    wl->applet = applet;
    gelogger_set( &(wl->gelogger) );

    //do not allow the applet to resize it self if icon disapears while flashing    
	gtk_window_set_resizable( (GtkWindow*)(wl->applet), FALSE );

	panel_applet_setup_menu (PANEL_APPLET (wl->applet),
                         Context_menu_xml,
                         gelogger_menu_verbs,
                         wl );

	g_timeout_add( 1000, check_absence, wl );


    //create edit box on panel
    
	GtkWidget   *frame  = NULL;
	GtkWidget   *hbox   = NULL;

//	debug( 0, wl->gelogger, time( NULL ), "start working detected" );

	frame = gtk_frame_new( NULL );
	wl->hbox = gtk_hbox_new (FALSE, 0);
	wl->icon = gtk_image_new_from_stock( GTK_STOCK_ABOUT, GTK_ICON_SIZE_SMALL_TOOLBAR );
	
	g_object_ref( (gpointer)wl->icon );
	
	wl->editbox = gtk_entry_new();
	gtk_entry_set_editable( GTK_ENTRY(wl->editbox), TRUE );
    
    //gain focus if mouse button is pressed over edit box
	g_signal_connect( wl->editbox, "button_press_event",
		      G_CALLBACK (want_input), wl );
    
    //check input for ENTER key and write entered text if ENTER pressed
	g_signal_connect ( wl->editbox, "key_press_event",
		      G_CALLBACK (command_key ), wl );
    
	g_signal_connect ( wl->applet, "destroy",
		      G_CALLBACK (finalize), wl );

	gtk_box_pack_start (GTK_BOX (wl->hbox), wl->icon, FALSE, FALSE, 5);
	gtk_box_pack_end (GTK_BOX (wl->hbox), wl->editbox, FALSE, FALSE, 5);

	gtk_window_set_resizable( (GtkWindow*)frame, FALSE );
	gtk_window_set_resizable( (GtkWindow*)wl->hbox, FALSE );

	gtk_container_add (GTK_CONTAINER (frame), wl->hbox);

	gtk_container_add (GTK_CONTAINER (wl->applet), frame);

	gtk_widget_show_all (GTK_WIDGET (wl->applet));

	return TRUE;
}



PANEL_APPLET_BONOBO_FACTORY ("OAFIID:GeloggerApplet_Factory",
                             PANEL_TYPE_APPLET,
                             "The gelogger Applet",
                             "0",
                             gelogger_applet_fill,
                             NULL);

